const logModel = require('../models/logModel');
const eventModel = require('../models/eventModel');

// 获取所有日志记录
async function getAllLogs(req, res, next) {
  try {
    const page = parseInt(req.query.page) || 1;
    const limit = parseInt(req.query.limit) || 10;
    
    const logs = await logModel.getAllLogs(page, limit);
    
    // 补充事件名称信息
    const logsWithEventInfo = await Promise.all(logs.data.map(async (log) => {
      const event = await eventModel.getEventById(log.eventId);
      return {
        ...log,
        eventName: event ? event.name : '未知事件'
      };
    }));
    
    res.json({
      ...logs,
      data: logsWithEventInfo
    });
  } catch (error) {
    next(error);
  }
}

// 获取特定事件的日志记录
async function getLogsByEventId(req, res, next) {
  try {
    const { eventId } = req.params;
    const page = parseInt(req.query.page) || 1;
    const limit = parseInt(req.query.limit) || 10;
    
    // 检查事件是否存在
    const event = await eventModel.getEventById(eventId);
    if (!event) {
      return res.status(404).json({
        success: false,
        message: '事件不存在'
      });
    }
    
    const logs = await logModel.getLogsByEventId(eventId, page, limit);
    
    // 添加事件名称
    const logsWithEventName = logs.data.map(log => ({
      ...log,
      eventName: event.name
    }));
    
    res.json({
      ...logs,
      data: logsWithEventName
    });
  } catch (error) {
    next(error);
  }
}

// 获取单个日志记录
async function getLogById(req, res, next) {
  try {
    const { id } = req.params;
    const log = await logModel.getLogById(id);
    
    if (!log) {
      return res.status(404).json({
        success: false,
        message: '日志记录不存在'
      });
    }
    
    // 获取事件信息
    const event = await eventModel.getEventById(log.eventId);
    
    res.json({
      ...log,
      eventName: event ? event.name : '未知事件'
    });
  } catch (error) {
    next(error);
  }
}

// 创建日志记录
async function createLog(req, res, next) {
  try {
    const logData = req.body;
    
    // 基本验证
    if (!logData.eventId) {
      return res.status(400).json({
        success: false,
        message: '缺少必要字段'
      });
    }
    
    // 验证事件是否存在
    const event = await eventModel.getEventById(logData.eventId);
    if (!event) {
      return res.status(400).json({
        success: false,
        message: '事件不存在'
      });
    }
    
    const newLog = await logModel.createLog(logData);
    
    res.status(201).json({
      ...newLog,
      eventName: event.name
    });
  } catch (error) {
    next(error);
  }
}

// 删除日志记录
async function deleteLog(req, res, next) {
  try {
    const { id } = req.params;
    const result = await logModel.deleteLog(id);
    
    if (!result) {
      return res.status(404).json({
        success: false,
        message: '日志记录不存在'
      });
    }
    
    res.json({
      success: true,
      message: '日志记录删除成功'
    });
  } catch (error) {
    next(error);
  }
}

// 清空特定事件的所有日志
async function clearEventLogs(req, res, next) {
  try {
    const { eventId } = req.params;
    
    // 检查事件是否存在
    const event = await eventModel.getEventById(eventId);
    if (!event) {
      return res.status(404).json({
        success: false,
        message: '事件不存在'
      });
    }
    
    await logModel.clearEventLogs(eventId);
    
    res.json({
      success: true,
      message: `事件 "${event.name}" 的所有日志已清空`
    });
  } catch (error) {
    next(error);
  }
}

module.exports = {
  getAllLogs,
  getLogsByEventId,
  getLogById,
  createLog,
  deleteLog,
  clearEventLogs
}; 